package com.ruoyi.web.tdmdata.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.tdmdata.mapper.TdmYaoDianShouLuMapper;
import com.ruoyi.web.tdmdata.domain.TdmYaoDianShouLu;
import com.ruoyi.web.tdmdata.service.ITdmYaoDianShouLuService;

/**
 * ID-04 -21 -31 药典收录Service业务层处理
 * 
 * @author ly
 * @date 2022-03-14
 */
@Service
public class TdmYaoDianShouLuServiceImpl implements ITdmYaoDianShouLuService 
{
	private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
	
    @Autowired
    private TdmYaoDianShouLuMapper tdmYaoDianShouLuMapper;
    
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询ID-04 -21 -31 药典收录
     * 
     * @param id ID-04 -21 -31 药典收录主键
     * @return ID-04 -21 -31 药典收录
     */
    @Override
    public TdmYaoDianShouLu selectTdmYaoDianShouLuById(Long id)
    {
        return tdmYaoDianShouLuMapper.selectTdmYaoDianShouLuById(id);
    }

    /**
     * 查询ID-04 -21 -31 药典收录列表
     * 
     * @param tdmYaoDianShouLu ID-04 -21 -31 药典收录
     * @return ID-04 -21 -31 药典收录
     */
    @Override
    public List<TdmYaoDianShouLu> selectTdmYaoDianShouLuList(TdmYaoDianShouLu tdmYaoDianShouLu)
    {
        return tdmYaoDianShouLuMapper.selectTdmYaoDianShouLuList(tdmYaoDianShouLu);
    }

    /**
     * 新增ID-04 -21 -31 药典收录
     * 
     * @param tdmYaoDianShouLu ID-04 -21 -31 药典收录
     * @return 结果
     */
    @Override
    public int insertTdmYaoDianShouLu(TdmYaoDianShouLu tdmYaoDianShouLu)
    {
        tdmYaoDianShouLu.setCreateTime(DateUtils.getNowDate());
        return tdmYaoDianShouLuMapper.insertTdmYaoDianShouLu(tdmYaoDianShouLu);
    }

    /**
     * 修改ID-04 -21 -31 药典收录
     * 
     * @param tdmYaoDianShouLu ID-04 -21 -31 药典收录
     * @return 结果
     */
    @Override
    public int updateTdmYaoDianShouLu(TdmYaoDianShouLu tdmYaoDianShouLu)
    {
        tdmYaoDianShouLu.setUpdateTime(DateUtils.getNowDate());
        return tdmYaoDianShouLuMapper.updateTdmYaoDianShouLu(tdmYaoDianShouLu);
    }

    /**
     * 批量删除ID-04 -21 -31 药典收录
     * 
     * @param ids 需要删除的ID-04 -21 -31 药典收录主键
     * @return 结果
     */
    @Override
    public int deleteTdmYaoDianShouLuByIds(Long[] ids)
    {
        return tdmYaoDianShouLuMapper.deleteTdmYaoDianShouLuByIds(ids);
    }

    /**
     * 删除ID-04 -21 -31 药典收录信息
     * 
     * @param id ID-04 -21 -31 药典收录主键
     * @return 结果
     */
    @Override
    public int deleteTdmYaoDianShouLuById(Long id)
    {
        return tdmYaoDianShouLuMapper.deleteTdmYaoDianShouLuById(id);
    }
    
    /**
     * 导入用户数据
     * 
     * @param userList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importUser(List<TdmYaoDianShouLu> dataList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(dataList) || dataList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TdmYaoDianShouLu data : dataList)
        {
            try
            {
            	switch (data.getTypeId()) {
            	case "复杂体系":
					data.setTypeId("1001");
					break;
				case "肿瘤药物":
					data.setTypeId("1002");
					break;
				case "过敏物":
					data.setTypeId("100101");
					break;
				default:
					data.setTypeId("10");
				}
            	 this.insertTdmYaoDianShouLu(data);
                 successNum++;
                 successMsg.append("<br/>" + successNum + "、药典 " + data.getZhongWenMingCheng() + " 导入成功");
             
//                // 验证是否存在这个用户
//                SysUser u = userMapper.selectUserByUserName(user.getUserName());
//                if (StringUtils.isNull(u))
//                {
//                    BeanValidators.validateWithException(validator, user);
//                    user.setPassword(SecurityUtils.encryptPassword(password));
//                    user.setCreateBy(operName);
//                   
//                else if (isUpdateSupport)
//                {
//                    BeanValidators.validateWithException(validator, user);
//                    user.setUpdateBy(operName);
//                    this.updateUser(user);
//                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
//                }
//                else
//                {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
//                }
//                
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、药典 " + data.getZhongWenMingCheng() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
